$(function () {
    /*树形组件*/
    layui.use(['tree', 'table', 'util', 'form'], function () {
        var layer = layui.layer
            , util = layui.util
            , table = layui.table
            , form = layui.form;

        var delRoleNos = [];

        /*请求获取角色列表*/
        table.render({
            elem: '#roleList'
            , height: 'full-20'
            , skin: 'line'
            , url: '/gzht/roleManager/getRoleList'
            , text: {
                none: '暂无相关数据'
            }
            , title: '角色列表'
            , page: true
            , limits: [5, 10, 20]
            , where: {}
            , toolbar: '#tools'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'roleNo', title: '编号'}
                , {field: 'roleName', title: '角色名称'}
                , {
                    field: 'roleStatus', title: '状态', templet: function (d) {
                        var status = "";
                            switch (d.roleStatus) {
                                case 0:
                                    status = "<span class=\"layui-badge layui-bg-green\">正常</span>"
                                    break;
                                case 1:
                                    status = "<span class=\"layui-badge layui-bg-gray\">禁用</span>"
                                    break;
                                default:
                                    status = "<span class=\"layui-badge layui-bg-orange\">异常</span>"
                                    break;
                            }
                        return status;
                    }
                }
                , {field: 'roleCreateTime', title: '创建时间'}
                , {field: 'roleRemark', title: '备注信息'}
                , {fixed: 'right', width: 165, align: 'center', toolbar: '#col_tool'}
            ]]
            , response: {
                statusName: 'code'
                , statusCode: 1001
                , msgName: 'msg'
                , countName: 'count'
                , dataName: 'data'
            }
            , parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
        });

        // 监听头部工具条
        var addIndex;
        table.on('toolbar(roleListTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var dataLen = checkStatus.data.length;
            var checkedData = checkStatus.data;
            switch (obj.event) {
                case 'h_role_add':
                    addIndex = layer.open({
                        type: 1,
                        area: '600px',
                        content: $('#addRoleArea')
                    });
                    break;
                case 'h_role_del':
                    if (dataLen == 0) {
                        layer.msg("请选择需要删除的行")
                    } else {
                        layer.confirm('确认删除选择的'+dataLen+'行么', function (index) {
                            var delNos = [];
                            for(var item of checkedData){
                                delNos.push(item.roleNo)
                            }
                            //向服务端发送删除指令
                            $.ajax({
                                url: '/gzht/roleManager/delRole',
                                type: 'post',
                                data: {
                                    roleNos: delNos
                                },
                                success: function (res) {
                                    if (res.code == 1001) {
                                        layer.msg('删除成功');
                                        table.reload('roleList')
                                        // obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                        layer.close(index);
                                    }
                                }
                            })
                        });
                    }
                    break;
            }
            ;
        });

        //监听行内工具条
        table.on('tool(roleListTable)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event;
            var tr = obj.tr;
            if (layEvent === 'edit') { //编辑
                window.location.href = '/gzht/roleEdit?roleNo='+data.roleNo
            } else if (layEvent === 'del') { //删除
                layer.confirm('真的删除行么', function (index) {
                    //向服务端发送删除指令
                    delRoleNos = [data.roleNo]
                    $.ajax({
                        url: '/gzht/roleManager/delRole',
                        type: 'post',
                        data: {
                            roleNos: delRoleNos
                        },
                        success: function (res) {
                            if (res.code == 1001) {
                                layer.msg('删除成功');
                                table.reload('roleList')
                                // obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                            }
                        }
                    })
                });
            }
        });
        form.on('switch(switchStatus)', function (obj) {
            layer.tips((this.checked ? '启用' : '禁用') + '用户成功', obj.othis)
        })

        /*监听添加角色提交*/
        form.on('submit(roleAddSubmit)', function(data){
            var data = JSON.stringify(data.field);
            console.log(data)
            $.ajax({
                url: '/gzht/roleManager/addRole',
                data: data,
                type: 'post',
                dataType: 'json',
                contentType: 'application/json',
                success: function (res) {
                    if(res.code == 1001){
                        layer.msg('添加成功', {
                            icon: 1,
                            time: 600
                        }, function(){
                            layer.close(addIndex);
                            location.reload();
                        });
                    }else {
                        layer.msg(res.desc);
                    }
                },
                error: function (err) {
                    layer.msg("网络出错")
                }
            })
            return false;
        });
    });

})
